#ifndef FMMTPLAN
#define FMMTPLAN

#include <GL/glut.h>
#include <math.h>

#include "sistema.h"
#include "ranhuras.h"
#include "miscfunc.h"
#include "menufpln.h"
#include "fmmtcir.h"

extern sistema ms;
extern fmmt estator, rotor, resul;
extern ranhuras est, rot;
extern bool showcmps,showres;

int janfmmtplan;
bool showfmmtplan=false, firstfmmtplan=true;
extern bool ok, step;

void fmmtplanDisplay(void)
{
	float y=110;
	glClearColor (PRETO,1.0);
	glClear (GL_COLOR_BUFFER_BIT);

	glColor3f(COR_GRAF_AXIS);
	glLineWidth(1);
	glBegin(GL_LINES);
		glVertex2f(0,0);
		glVertex2f(TAM_J3_X,0);
	glEnd();

	if(showcmps)
	{
		glColor3f(COR_VETOR_FE);
		estator.drawfmmtplan(0,0,(double)TAM_J3_X/360,1);
		glBegin(GL_QUADS);
			glVertex2f(5, y);
			glVertex2f(5, y+10);
			glVertex2f(15, y+10);
			glVertex2f(15, y);
		glEnd();

		glColor3f(COR_VETOR_FR);
		rotor.drawfmmtplan(0,0,500.0/360,1);
		glBegin(GL_QUADS);
			glVertex2f(5, y-20);
			glVertex2f(5, y-10);
			glVertex2f(15, y-10);
			glVertex2f(15, y-20);
		glEnd();

		glColor3f(LEG_COLOR);
		outtext(LEFT, LEG_FONT, 25, y, MSG62);
		outtext(LEFT, LEG_FONT, 25, y-20, MSG63);
	}

	if(showres)
	{
		glColor3f(COR_VETOR_FRES);
		resul.drawfmmtplan(0,0,500.0/360,1);
		glBegin(GL_QUADS);
			glVertex2f(TAM_J3_X-120, y);
			glVertex2f(TAM_J3_X-120, y+10);
			glVertex2f(TAM_J3_X-110, y+10);
			glVertex2f(TAM_J3_X-110, y);
		glEnd();
		glColor3f(LEG_COLOR);
		outtext(LEFT, LEG_FONT, TAM_J3_X-100, y, MSG63A);
	}

	glutSwapBuffers();
}

void fmmtplanReshape (int w, int h)
{
	glutReshapeWindow(TAM_J3_X, TAM_J3_Y);
	glViewport (0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glOrtho (0.0, (GLdouble) TAM_J3_X, 0.0, (GLdouble) TAM_J3_Y, -1, 1);
	glTranslatef(0, (GLdouble) TAM_J3_Y/2, 0);
}

void fmmtplanKeyboard(unsigned char key, int x, int y)
{
	switch (key) 
	{
	case 9:
		glutFullScreen();
		break;
	case 'r':
		vetoresReshape(TAM_J3_X,TAM_J3_Y);
		break;
	case ' ':
		ok=!ok;
		break;
	case 13:
		step=true;
		break;
	case 'p':
		ms.report();
		break;
	case 'e':
		ms.setIEXC(ms.iexc+2.5);
		controleRedisplay();
		break;
	case 'd':
		ms.setIEXC(ms.iexc-2.5);
		controleRedisplay();
		break;
	case 'q':
		ms.setPT(ms.pt+100000);
		controleRedisplay();
		break;
	case 'a':
		ms.setPT(ms.pt-100000);
		controleRedisplay();
		break;
	case 't':
		ms.go(20000);
		break;
	case 27:
		glutSetWindow(janfmmtplan);
		glutHideWindow();
		showfmmtplan=false;
		break;
	}
}

int fmmtplanCreate(void)
{
	glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowSize (TAM_J3_X,TAM_J3_Y);
    glutInitWindowPosition (0, 0);
    janfmmtplan=glutCreateWindow (MSG64); 

    glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel (GL_FLAT);

	glutDisplayFunc(fmmtplanDisplay);
    glutReshapeFunc(fmmtplanReshape);
    glutKeyboardFunc(fmmtplanKeyboard);
	fmmtplanCreateMenu();
    return 0;
}

#endif